package com.nemodynamic.cattivial.facebook;

import com.facebook.FacebookRequestError;
import com.nemodynamic.cattivial.R;
import com.nemodynamic.cattivial.facebook.FacebookUtils.OnFBSessionChangeListener;
import com.nemodynamic.cattivial.utils.DLog;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

public class FacebookPostButton extends Button {
	private static final String TAG = "FacebookPostButton";
	private String attrPost[];
	private boolean isAutomanagedVisibility;
	private OnFBSessionChangeListener listener;
	private OnFBSessionChangeListener defaultListener;
	private OnFBPostListener postListener;
	private boolean isDetouchedPerformed = false;
	private Activity activity;

	public FacebookPostButton(Context context) {
		super(context);
		DLog.i(TAG, "DefList:" + defaultListener + "List:" + listener);
	}

	public FacebookPostButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(attrs);
		DLog.i(TAG, "DefList:" + defaultListener + "List:" + listener);
	}

	public FacebookPostButton(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(attrs);
		DLog.i(TAG, "DefList:" + defaultListener + "List:" + listener);
	}

	public void setDefaultOnChangeSessionListener() {
		if (defaultListener == null) {
			defaultListener = new OnFBSessionChangeListener() {

				@Override
				public void onFBOpeningSession() {
					if (isAutomanagedVisibility) {
						setVisibility(View.INVISIBLE);
					}
				}

				@Override
				public void onFBOpenedSessionTokenUpdated() {

				}

				@Override
				public void onFBOpenedSession() {
					if (isAutomanagedVisibility) {
						setVisibility(View.INVISIBLE);
					}
				}

				@Override
				public void onFBCreatedSessionTockenLoaded() {
					if (isAutomanagedVisibility) {
						setVisibility(View.INVISIBLE);
					}
				}

				@Override
				public void onFBCreatedSession() {
					setVisibility(View.VISIBLE);
				}

				@Override
				public void onFBClosedSessionLogInFailed() {
					if (isAutomanagedVisibility) {
						setVisibility(View.INVISIBLE);
					}
				}

				@Override
				public void onFBClosedSession() {
					if (isAutomanagedVisibility) {
						setVisibility(View.INVISIBLE);
					}
				}
			};
		}
	}

	public void removeDefaultOnChangeSessionListener() {
		if (defaultListener != null) {
			DLog.i(TAG, "removeOnChangeSessionListener");
			FacebookUtils.removeOnChangeSessionListener(defaultListener);
		}
	}

	public void setOnChangeSessionListener(OnFBSessionChangeListener listener, Activity activity) {
		DLog.i(TAG, "setOnChangeSessionListener");
		this.activity = activity;
		this.listener = listener;
		FacebookUtils.addOnChangeSessionListener(listener, activity);
	}

	public void removeOnChangeSessionListener(OnFBSessionChangeListener listener) {
		DLog.i(TAG, "removeOnChangeSessionListener");
		FacebookUtils.removeOnChangeSessionListener(listener);
	}

	public void setFBOnClickListener(final OnClickListener l, final Activity activity) {
		super.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				l.onClick(v);
				//TODO:add dialog
				FacebookUtils.publishStory(activity, attrPost, postListener);
				
				
			}
		});
	}

	public void initButton(final Activity activity) {
		DLog.i(TAG, "initButton");
		setDefaultOnChangeSessionListener();
		setFBOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		}, activity);
	}

	public void setFBOnPostListener(final OnFBPostListener postListener) {
		this.postListener = postListener;
	}

	@Override
	protected void onAttachedToWindow() {
		if (activity != null && listener != null) {
			setOnChangeSessionListener(listener, activity);
		}
		if (activity != null && defaultListener != null) {
			FacebookUtils.addOnChangeSessionListener(defaultListener, activity);
		}
		DLog.i(TAG, "Attached: defaultListener=" + defaultListener + " listener=" + listener);

		super.onAttachedToWindow();
	}

	@Override
	protected void onDetachedFromWindow() {
		DLog.i(TAG, "Detached: defaultListener=" + defaultListener + " listener=" + listener);
		if (defaultListener != null) {
			removeDefaultOnChangeSessionListener();
		}
		if (listener != null) {
			FacebookUtils.removeOnChangeSessionListener(listener);
		}
		isDetouchedPerformed = true;
		super.onDetachedFromWindow();
	}

	private void init(AttributeSet attrs) {

		this.attrPost = new String[5];
		TypedArray style = getContext().obtainStyledAttributes(attrs, R.styleable.FacebookButtonView);
		this.attrPost[0] = style.getString(R.styleable.FacebookButtonView_name);
		this.attrPost[1] = style.getString(R.styleable.FacebookButtonView_caption);
		this.attrPost[2] = style.getString(R.styleable.FacebookButtonView_description);
		this.attrPost[3] = style.getString(R.styleable.FacebookButtonView_pagelink);
		this.attrPost[4] = style.getString(R.styleable.FacebookButtonView_picturelink);
		this.isAutomanagedVisibility = style.getBoolean(R.styleable.FacebookButtonView_automanagedvisibiity, false);
		if (FacebookUtils.isSessionOpened(getContext())) {
			setVisibility(View.VISIBLE);
		} else {
			if (isAutomanagedVisibility) {
				setVisibility(View.INVISIBLE);
			}
		}
	}

	public interface OnFBPostListener {
		public void onFBnoLog();

		public void onFBPosting();

		public void onFBPostedSuccess(String postId);

		public void onFBPostedError(FacebookRequestError error);

	}

	@Override
	protected void onFinishInflate() {

		if (FacebookUtils.isSessionOpened(getContext())) {
			setVisibility(View.VISIBLE);
		} else {
			if (isAutomanagedVisibility) {
				setVisibility(View.INVISIBLE);
			}
		}

	}

	@Override
	public void onRestoreInstanceState(Parcelable state) {
		DLog.i(TAG, "Restore" + "DefList:" + defaultListener + "List:" + listener);
		super.onRestoreInstanceState(state);
		if (FacebookUtils.isSessionOpened(getContext())) {
			setVisibility(View.VISIBLE);
		} else {
			if (isAutomanagedVisibility) {
				setVisibility(View.INVISIBLE);
			}
		}

	}

	@Override
	public void onWindowFocusChanged(boolean hasWindowFocus) {
		super.onWindowFocusChanged(hasWindowFocus);
		if (FacebookUtils.isSessionOpened(getContext())) {
			setVisibility(View.VISIBLE);
		} else {
			if (isAutomanagedVisibility) {
				setVisibility(View.INVISIBLE);
			}
		}
	}

	public void setName(String name) {
		attrPost[0] = name;
	}

	public void setName(int resId) {
		attrPost[0] = getContext().getString(resId);
	}

	public String getName() {
		return attrPost[0];
	}

	public void setCaption(String caption) {
		attrPost[1] = caption;
	}

	public void setCaption(int resId) {
		attrPost[1] = getContext().getString(resId);
	}

	public String getCaption() {
		return attrPost[1];
	}

	public void setDescription(String description) {
		attrPost[2] = description;
	}

	public void setDescription(int resId) {
		attrPost[2] = getContext().getString(resId);
	}

	public String getDescription() {
		return attrPost[2];
	}

	public void setPageLink(String pageLink) {
		attrPost[3] = pageLink;
	}

	public void setPageLink(int resId) {
		attrPost[3] = getContext().getString(resId);
	}

	public String getPageLink() {
		return attrPost[3];
	}

	public void setPictureLink(String pictureLink) {
		attrPost[4] = pictureLink;
	}

	public void setPictureLink(int resId) {
		attrPost[4] = getContext().getString(resId);
	}

	public String getPictureLink() {
		return attrPost[4];
	}

}
